﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;


namespace MedicWeb.forms.Directory {
    public partial class SimpleEdit : SessionPage {
        protected void Page_Load(object sender, EventArgs e) {
            string dir = Request.QueryString["directory"];

            if (dir == null || dir.Length == 0) {
                Response.Redirect("~/forms/DoctorList.aspx");
            //    //STUB
            //    var ms1 = new MainService();
            //    var session = ms1.Authorize("user", "sha");
            //    if (session != null) {
            //        Session[SessionKey] = session;
            //    }
            //    dir = "AcademicTitle";
            //    //ENDSTUB
            }
            lblTitle.Text = dir;
            var ms = new MainService();
            var items = new List<DirectorySimpleItem>();
            TreeView1.Nodes.Clear();
            switch (dir) {
                case "AcademicTitle": {
                        var list = ms.GetAcademicTitleList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
                case "Class": {
                        var list = ms.GetClassList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
                case "ClassSpecialisation": {
                        var list = ms.GetClassSpecialisationList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
                case "Degree": {
                    var list = ms.GetDegreeList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
                case "InstitutionType": {
                    var list = ms.GetInstitutionTypeList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
                case "JobCategory": {
                    var list = ms.GetJobCategoryList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
                case "Ownership": {
                    var list = ms.GetOwnershipList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
                case "Region": {
                    var list = ms.GetRegionList(GetSession(), false);
                        items.AddRange(list.ToList());
                        break;
                    }
            }
            var root = new TreeNode(dir);
            root.SelectAction = TreeNodeSelectAction.None;
            TreeView1.Nodes.Add(root);
            foreach (var simpleItem in items) {
                var node = new ObjectTreeNode<DirectorySimpleItem>(simpleItem);
                node.Text = simpleItem.Title;
                node.Value = "" + simpleItem.Id;
                root.ChildNodes.Add(node);
            }
            TreeView1.ExpandAll();


            //STUB
            for (int i = 0; i < 300; i++) {
                var tableRow = new TableRow();
                for (int j = 0; j < 5; j++) {
                    var tableCell = new TableCell();
                    var hyperLink = new HyperLink();
                    hyperLink.Text = "link " + i + "-" + j;
                    hyperLink.Target = "#";
                    tableCell.Controls.Add(hyperLink);
                    tableRow.Cells.Add(tableCell);
                }
                TableTest.Rows.Add(tableRow);
            }

            //ENDSTUB
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) {
            //LpuList.ObjectTreeNode<DirectorySimpleItem> node = (LpuList.ObjectTreeNode<DirectorySimpleItem>) TreeView1.SelectedNode;
            //DirectorySimpleItem directorySimpleItem = node.Obj;
            TextBox1.Text = TreeView1.SelectedValue;
            TextBox2.Text = TreeView1.SelectedNode.Text;
            TreeView1.ExpandAll();
        }

        protected void btSave_Click(object sender, EventArgs e) {

        }
    }
    public class ObjectTreeNode<T> : TreeNode where T : DirectorySimpleItem {
        private readonly T obj;

        public ObjectTreeNode(T obj)
            : base(obj.Title, "" + obj.Id) {
            this.obj = obj;
        }

        public T Obj {
            get { return obj; }
        }
    }
}